﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO;
using Spotify;
using MediaChrome;
namespace Spoticore
{
    public class Output
    {
        public static void Write(String instance, String values)
        {
            using (StreamWriter sw = new StreamWriter(Program.baseDirectory + "\\" + instance + ".command", true))
            {
                sw.Write(values);
                sw.Close();
            }
        }
    }
    class Program
    {
        public static string baseDirectory = ("C:\\spoticore");
        static void parse(String str)
        {
            // get command
            String[] c = str.Split(' ');

            String c1 = c[1];
            Link targetLink = null;
            try{
              targetLink =  Link.Create(c1);
            }
            catch(Exception e)
            {
            }
          
            // get command
            String command = c[0];
            switch (command)
            {
                case "PLAY":
                    try{
                    session.PlayerLoad(Track.CreateFromLink(targetLink));
                    session.PlayerPlay(true);
                    Output.Write(str,"OK");
                    }
                    catch(Exception e)
                    {
                        Output.Write(str,"ERROR " + e);
                    }
                    break;
                case "STOP":
                    session.PlayerPlay(false);
                    break;
                case "SEEK":
                    int pos = int.Parse(c[1]);
                    session.PlayerSeek(pos);
                    Output.Write(str,"OK");
                    break;
                case "PLAYLIST":
                    Playlist pls = Playlist.Create(session,targetLink);
                    // Wait for the playlist to load
                    while(pls!=null)
                        while(!pls.IsLoaded);
                     foreach(Track track in pls.CurrentTracks)
                     {
                           Output.Write(str,track.ToString());
                     }
                     break;

            }

            
        }
        public static int duration;
        public static int position;
        public static BassPlayer player;
        static Spotify.Session session;
        static void Main(string[] args)
        {
            // Create session
            session = Session.CreateInstance(new Spocky.MyClass().AppKey(), "C:\\spoticache", "C:\\spoticache", "linspot");
            session.LogIn(args[0], args[1]);
            session.OnMusicDelivery += new MusicDeliveryEventHandler(session_OnMusicDelivery);
            while (true)
            {
                if (Directory.Exists(baseDirectory))
                {
                    DirectoryInfo di = new DirectoryInfo(baseDirectory);
                    foreach (FileInfo fi in di.GetFiles(".command"))
                    {
                        String fileName = fi.Name;
                        // parse command
                    }
                }
                
                // sleep the thread
                Thread.Sleep(100);
            }
        }

        static void session_OnMusicDelivery(Session sender, MusicDeliveryEventArgs e)
        {
            if (e.Samples.Length > 0)
            {

                if (player == null)
                {
                    //player = new AlsaPlayer(e.Rate / 2); // Buffer 500ms of audio
                    player = new BassPlayer();

                    Console.WriteLine("Player created");
                }


                // Don't forget to set how many frames we consumed
                e.ConsumedFrames = player.EnqueueSamples(new AudioData(e.Channels, e.Rate - 12, e.Samples, e.Frames));

                /*X.Maximum = (float)currentTrack.Duration;
                X.Value  +=10;*/
                duration = e.Frames;
                position = e.ConsumedFrames;

            }
            else
            {
                e.ConsumedFrames = 0;
            }
        }
    }
}
